import 'package:flutter/material.dart';
import 'package:my_app/tools/http_server.dart';
import 'package:my_app/conponate/user_info.dart';

class StarListTile extends StatefulWidget {
  final String gname;
  final String type;
  const StarListTile({required this.gname, required this.type});

  @override
  _StarListTileState createState() {
    return _StarListTileState(this.gname,this.type);
  }
}

class _StarListTileState extends State<StarListTile> with TickerProviderStateMixin{
  late String _gname;
  late String _type;
  _StarListTileState(String gname,String type){
    this._gname = gname;
    this._type = type;
  }

  bool isChanged = false;
  late AnimationController _controller;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _controller = AnimationController(vsync: this,
        duration: Duration(seconds: 1));
  }

  void trigger(){
    setState(() {
      isChanged = !isChanged;
      isChanged ? _controller.forward() : _controller.reverse();
      showDialog<Null>(
          context: context,
          barrierDismissible: false,
          builder: (BuildContext context) {
            HttpServer.star(UserInfo.userName, _gname, "取消收藏");
            return new AlertDialog(
              title: new Text('已取消收藏'),
              actions: <Widget>[
                new FlatButton(
                  child: new Text('确定'),
                  onPressed: () {
                    Navigator.of(context).pop();
                  },
                ),
              ],
            );
          }
      ).then((val) {
        print(val);
      }
      );
    },
    );
  }

  @override
  Widget build(BuildContext context) {
    return ListTile(title: Text(_gname),
      subtitle: Text(_type),
      trailing: IconButton(
        iconSize: 30,
        onPressed: trigger,
        icon: AnimatedIcon(
          icon:AnimatedIcons.menu_close,
          color:Colors.blueGrey,
          progress: _controller,
        ),
      ),);
  }
}
